<?php


namespace app\admin\controller;

use app\logic\DataHelper;
use app\logic\DeptHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;

class Admin extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function save() {
        $params = get_all_params();


        if (!check_params_empty(['login_name', 'name', 'phone'])) {
            return json([ 'code' => -1, 'msg' => '请填写：登录名,姓名,手机号' ]);
        }

        if (isset($params['role_id'])) {
            $params['role_name'] = DataHelper::get_value('tb_role', "id=" . $params['role_id'], 'title');
        }

        if (isset($params['id']) && $params['id'] > 0) {
            //更新
            unset($params['login_name']);
            DataHelper::update('tb_admin', "id=".$params['id'], $params);
        } else {

            //判断登录名是否已存在
            if (DataHelper::is_exist('tb_admin', [ [ "login_name", "=", $params["login_name"] ] ])) {
                return json([ 'code' => -1, 'msg' => '登录名已存在' ]);
            }

            //新增
            $params['password'] = md5('123456');
            DataHelper::add('tb_admin', $params);
        }

        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function load_dept() {
        $tree = DeptHelper::load_dept(0);

//        $tree = array(
//            [
//                'id' => 0,
//                'pid' => 0,
//                'pname' => '',
//                'title' => '根节点',
//                'link' => '',
//                'status' => 0,
//                'spread' => true,
//                'children' => $tree
//            ]
//        );

        return json([
            'code' => 0,
            'msg' => '成功',
            'tree' => $tree
        ]);
    }

    public function edit($id = 0) {
        if ($id > 0) {
            $entry = DataHelper::find_by_id('tb_admin', $id);
        } else {
            $entry = [
                'id' => 0,
                'login_name' => '',
                'name' => '',
                'phone' => '',
                'email' => '',
                'sex' => '',
                'status' => 0,
                'role_id' => '',
                'role_name' => '',
                'dept_id' => '',
                'dept_name' => ''
            ];
        }

        //角色列表
        $role_list = DataHelper::select('tb_role', "status=0");

        return View::fetch('edit', [
            'entry' => $entry,
            'role_list' => $role_list
        ]);
    }

    public function list($name = '', $phone = '', $page = 1, $limit = 20) {
        $query = " status=0 ";

        if (!empty($name)) {
            $query .= " and name like '%$name%' ";
        }

        if (!empty($phone)) {
            $query .= " and phone like '%$phone%' ";
        }


        list($list, $count) = DataHelper::select_page('tb_admin', $query, [ 'page' => $page, 'limit' => $limit ]);

        foreach ($list as &$item) {
            $item['sex'] = ($item['sex'] == 1 ? '男' : '女');
        }

        if ($count <= 0) {
            return json([ 'code' => 201, 'msg' => '没有相关数据' ]);
        }

        return json([
            'code' => 0,
            'msg' => '成功',
            'count' => $count,
            'data' => $list
        ]);
    }

    public function del() {
        $params = get_all_params();
        $ids = $params['ids'];

        DataHelper::update('tb_admin', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }
}